//
//  MY.m
//  MMLOOK
//
//  Created by dllo on 15/8/24.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "MY.h"
#import "MyTableViewCell.h"
#import "CollectViewController.h"
#import "AppDelegate.h"
#import "NSDictionary+mutableDeepCopy.h"
#import "MyMusicViewController.h"

@interface MY ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>

//@property (nonatomic, retain)UITableView *table;
@property (nonatomic, retain)NSMutableArray *Array;
@property (nonatomic, retain)UIView *dark;
@end

@implementation MY

- (void)dealloc
{
    
    [_dark release];
    [_Array release];
    [_table release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.Array = [NSMutableArray arrayWithObjects:@"我的收藏", @"夜间模式", @"清除缓存", @"版本更新", @"关于我们", nil];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:253 / 255.0 green:180/255.0 blue:83/255.0 alpha:1];

    self.navigationItem.title = @"我的";
 

    UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * OffHeight)] autorelease];
    imageView.image = [UIImage imageNamed:@"tou.jpg"];
 
    [self.view addSubview:imageView];
    
    [self creatTableView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
    
    self.tabBarController.tabBar.translucent = NO;
    
}

- (void)creatTableView
{
    UIView *tableview = [[[UIView alloc] initWithFrame:CGRectMake(0, 210 * OffHeight, self.view.frame.size.width, self.view.frame.size.height - 220 * OffHeight)] autorelease];
    self.table = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 210 * OffHeight) style:UITableViewStylePlain] autorelease];
 //_table.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"11.jpg"]];
    _table.dataSource = self;
    _table.delegate = self;
    //_table.rowHeight = 40 * OffHeight;
    [tableview addSubview:_table];
    [self.view addSubview:tableview];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *s = @"";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:s];
    if (!cell) {
        cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:s] autorelease];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lable.text = [_Array objectAtIndex:indexPath.row];
    if (indexPath.row == 1) {
        UISwitch *sw = [[[UISwitch alloc] init] autorelease];
        [sw addTarget:self action:@selector(swAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryView = sw;
        self.dark = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        self.dark.backgroundColor = [UIColor blackColor];
        self.dark.alpha = 0.6;
        _dark.userInteractionEnabled = NO;
    }
    //显示最右方箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        CollectViewController *collect = [[[CollectViewController alloc] init] autorelease];
        [self.navigationController pushViewController:collect animated:YES];
    }else if (indexPath.row == 2) {
        NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *path = [array lastObject];
        NSString *str = [NSString stringWithFormat:@"已清除缓存%.1fM",[self folderSizeAtPath:path]];
        NSArray *files = [[NSFileManager defaultManager]subpathsAtPath:path];
        for (NSString *p in files) {
            NSError *error;
            NSString *Path = [path stringByAppendingPathComponent:p];
            if ([[NSFileManager defaultManager] fileExistsAtPath:Path]) {
                [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
            }
        }
        UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"友情提示" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alrt show];
        [alrt release];
    } else if (indexPath.row == 3) {
        UIAlertView *alart = [[[UIAlertView alloc] initWithTitle:@"友情提示" message:@"此版本为当前最新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]autorelease];
        [alart show];
    } else if (indexPath.row == 4) {
        UIAlertView *alart = [[[UIAlertView alloc] initWithTitle:@"温馨互动" message:@"由吧啦吧啦个人团队强力打造" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil] autorelease];
        [alart show];
        
    }
        

}

- (void)swAction: (UISwitch *)sw
{
    if (sw.on == YES) {
        AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [app.window addSubview:_dark];
    } else{
        [_dark removeFromSuperview];
    }
}

-(long long)fileSizeAtPath: (NSString *)filePath
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]) {
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

//遍历文件夹，获得文件夹大小，返回多少M
- (float)folderSizeAtPath: (NSString *)folderPath
{
    NSFileManager *file = [NSFileManager defaultManager];
    if (![file fileExistsAtPath:folderPath]) {
        return 0;
    }
    // 读取某个文件
    NSEnumerator *child = [[file subpathsAtPath:folderPath] objectEnumerator];
    NSString *fileName;
    long long folderSize = 0;
    while ((fileName = [child nextObject]) != nil) {
        NSString *filepath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath: filepath];
    }
    return folderSize / (1024.0 * 1024.0);
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
